<template>
  <div class="header">
    <a href="/">
      <img class="layout-img" src="../../assets/logo.png" alt="" />
      <span class="layout-title">梦学谷会员管理平台</span>
    </a>
    <el-dropdown @command="handleClick">
      <span class="el-dropdown-link">
        {{ username }}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="pwd">修改密码</el-dropdown-item>
        <el-dropdown-item command="getOut">退出登录</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="500px">
      <el-form
        :model="passForm"
        ref="dialogForm"
        status-icon
        :rules="rules"
        style="width: 400px"
        label-width="100px"
      >
        <el-form-item label="原密码" prop="oldPass">
          <el-input
            type="password"
            v-model="passForm.oldPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input
            type="password"
            v-model="passForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="passForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPass">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import store from "../../store";
import userApi from "../../api/user";
export default {
  data() {
    let validateOldPass = async (rule, value, callback) => {
      let res = await userApi.validPass(this.userId, value);
      if (res.data.flag) {
        callback();
      } else {
        callback(new Error(res.data.message));
      }
    };
    let validatePass = (rule, value, callback) => {
      if (value == this.passForm.pass) {
        callback();
      } else {
        callback(new Error("两次密码输入不一致"));
      }
    };
    return {
      username: store.state.userInfo.name,
      passForm: {
        oldPass: "",
        pass: "",
        checkPass: "",
      },
      dialogFormVisible: false,
      userId: store.state.userInfo.id,
      rules: {
        oldPass: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
          { validator: validateOldPass, trigger: "blur" },
        ],
        pass: [{ required: true, message: "新密码不能为空", trigger: "blur" }],
        checkPass: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    handleClick(val) {
      switch (val) {
        case "pwd":
          this.changePass();
          break;
        case "getOut":
          this.getOut();
          break;
      }
    },
    changePass() {
      this.dialogFormVisible = true;
    },
    submitPass() {
      this.$refs['dialogForm'].validate(async valid=>{
        if(valid){
          let res = await userApi.editPass(this.userId, this.passForm.pass)
          if(res.data.flag){
            this.$message.success(res.data.message);
            this.getOut();
          }else{
            this.$message.error(res.data.message);
          }
        }
      })
    },
    getOut() {
      store
        .dispatch("getOut")
        .then((res) => {
          if (res.data.flag) {
            this.$message.success(res.data.message);
            this.$router.push("/login");
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>

<style scoped >
.layout-img {
  width: 25px;
  height: 25px;
  vertical-align: middle;
  padding-right: 15px;
}
.layout-title {
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  vertical-align: middle;
}
.el-dropdown {
  float: right;
  color: #fff;
}
</style>
